package com.gym.code.generator.impl.dao.sql.impl;

import java.util.List;

import com.gym.code.generator.impl.dao.sql.WhereSqlGenerator;
import com.gym.code.generator.model.mapping.field.FieldGenerateModel;
import com.gym.code.generator.model.mapping.field.ParameterFieldGenerateModel;
import com.gym.code.generator.model.mapping.method.MethodUpdateModel;
import com.gym.code.generator.utils.LocalStringUtils;

/**
 * 
 * @author xinchun.wang 
   @email: 532002108@qq.com
 * @createTime 2015-4-15 下午8:21:45
 */
public class UpdateSqlGenerator extends WhereSqlGenerator<MethodUpdateModel> {

	private static UpdateSqlGenerator instance = new UpdateSqlGenerator();
	public static UpdateSqlGenerator getInstance(){
		return instance;
	}
	private UpdateSqlGenerator(){}
	
	@Override
	public String generate(MethodUpdateModel methodModel) {
		
		StringBuilder sqlBuilder = new StringBuilder();
		sqlBuilder.append("update  ").append(methodModel.getBeanGenerateModel().getTableName());
		sqlBuilder.append(" set ");
		sqlBuilder.append(generateSetColumn(methodModel.getUpdateFieldList()));
		sqlBuilder.append(" where 1 = 1 ");
		sqlBuilder.append(generateWhereColumn(methodModel.getParameterList()));
		return sqlBuilder.toString();
	}

	
	
	private String generateSetColumn(List<ParameterFieldGenerateModel> updateFieldList) {
		StringBuilder result = new StringBuilder();
		for(FieldGenerateModel item : updateFieldList){
			result.append(item.getColumnName()).append(" = ?,");
		}
		return LocalStringUtils.removeLastChar(result.toString(),',');
	}
	
}



